<?php 

namespace App\Http\Controllers\Admin;

use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Admin\ThrottlesLogins;
use Illuminate\Foundation\Admin\AuthenticatesAndRegistersUsers;
use Illuminate\Http\Request;
use App\Http\Requests;
use DB;

class BrandController extends Controller
{
	public $model=false;

	public function __construct(){

	      $this->model = new \App\Brand();
	}
	//品牌列表
	public function brand_list(){
		 $brand_name = isset($_GET['brand_name'])?$_GET['brand_name']:'';

		 $info= $this->model->lists($brand_name);
		 return view('Admin/brand/brand_list',['info'=>$info,'brand_name'=>$brand_name]);
		
	}

	//	品牌添加
   public function brand_add(){
      $req = request();//拿到类的对像
		if($_POST){
			 $time=time();
			 $req->file('brand_logo')->move('admin/img',$time.$_FILES['brand_logo']['name']);
			 $path = '/admin/img/'.$time.$_FILES['brand_logo']['name'];
			 $_POST['path'] = $path;
			 $res=$this->model->add($_POST);
			 if($res){
			 	echo '<script>alert("添加品牌成功");window.location.href="brand_list";</script>';
			 }else{
			 	echo '<script>alert("添加品牌失败");window.location.href="brand_add";</script>';
			 }
		}else{
		  	return view('Admin/brand/brand_add');
		}
   }

   //品牌修改
   public function brand_edit($brand_id){
	   		
	   	if(empty($_POST)){
	   		$listOne=$this->model->findOne($brand_id);
	  	 	return view('Admin/brand/brand_edit',['listOne'=>$listOne]);
	   	}else{
	   		$req = request();//拿到类的对像
	   		$time=time();
	   		if(!empty($_FILES['brand_logo']['name'])){
		   		$req->file('brand_logo')->move('admin/img',$time.$_FILES['brand_logo']['name']);
		   		$path = '/admin/img/'.$time.$_FILES['brand_logo']['name'];
	   			$_POST['path'] = $path;
	   		}else{
	   			$_POST['path'] = $_POST['brand_img'];
	   		}
	   		$res=$this->model->edit($_POST);
	   		if($res!=0){
	   			return redirect('/admin/brand_list');
	   		}else{
	   			return '<script>alert("修改品牌失败");location.href="brand_add";</script>';
	   		}
	   	}

   }

//品牌删除
   public function brand_del($brand_id){
   		$res=$this->model->del($brand_id);
   		if($res!=0){
   			return redirect('/admin/brand_list');
   		}else{
   			return redirect('/admin/brand_list');
   		}
   }

   //品牌还原
   public function brand_back($brand_id){
		   	$res=$this->model->back($brand_id);
		   	if($res!=0){
		   		return redirect('/admin/brand_list');
		   	}else{
		   		return redirect('/admin/brand_list');
		   	}
   }

   
}



 ?>